<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <el-table :data="listData" border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <!-- 在列中 可以通过插槽的形式 自定义这一列要渲染成什么样子 -->
        <el-table-column label="操作">
          <!-- v-slot：defalut='obj'  -->
          <!--简写  v-slot='obj' -->
          <!-- #default='obj' -->

          <template v-slot="obj">
            <el-button type="primary" size="mini" @click="editCate(obj.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="remove(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 弹窗 关闭弹窗  要重置表单 添加colsed 事件 -->
      <el-dialog title="提示" :visible.sync="addVisible" width="30%" @closed="addclosed">
        <!-- 点击添加 添加的表单项 -->
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 修改的弹窗 -->
      <el-dialog title="修改" :visible.sync="editVisible" width="30%">
        <!-- 点击添加 添加的表单项 -->
        <el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="70px">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="editForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="editForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateDate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  created() {
    this.initArtCate()
  },
  data() {
    // 文章分类列表的数据 默认空数组
    return {
      listData: [],
      // 控制添加对话框的显示与隐藏 false 是隐藏 true是显示
      addVisible: false,
      // 表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 表单的验证规则
      addFormRules: {
        cate_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          },
          {
            pattern: /^\S{1,10}$/,
            message: '名称必须1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          {
            required: true,
            message: '请输入分类别名',
            trigger: 'blur'
          },
          {
            pattern: /^\w{1,10}$/,
            message: '名称必须1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      },
      editVisible: false,
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑
      isShowEdit: false
    }
  },
  methods: {
    async initArtCate() {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return this.$message.error('获取信息失败')
      this.listData = res.data
      // console.log(res)
    },
    addclosed() {
      this.$refs.addFormRef.resetFields()
    },
    addCate() {
      // 预效验
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error('更新数据失败')
        this.$message.success('更新数据成功')
        // 提示完用户需要 关闭弹窗 刷新数据
        this.initArtCate()
        this.addVisible = false
      })
    },
    editCate(row) {
      if (row.id === 1 || row.id === 2) {
        // 如果布尔值为true
        if (this.isShowEdit) return
        this.$message({
          message: '管理员不允许修改这个分类!',
          type: 'error',
          onClose: () => {
            // 当前弹窗关闭的时候,把布尔值改为false
            // 目的:方便下次弹窗
            this.isShowEdit = false
          }
        })
        // this.$message.error('管理员不允许修改')
        return
      }
      // 把点击行的数据对象 ，直接复制给editForm  由于这里的row 是双向数据绑定  输入的时候数据变 表中的数据也跟着动 所以用浅拷贝
      // this.editForm = row
      this.editForm = Object.assign({}, row)
      // 展示修改分类的对话框
      this.editVisible = true
    },
    updateDate() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error('更新数据失败')
        this.$message.success('更新数据成功')
        this.initArtCate()
        this.editVisible = false
      })
    },
    async remove(id) {
      if (id === 1 || id === 2) {
        this.$message.error('管理员不允许删除')
        return
      }
      // 返回值是一个promise 对象
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (result === 'cancel') return
      const { data: res } = await this.$http.delete('/my/cate/del', {
        params: {
          id
        }
      })
      if (res.code !== 0) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.initArtCate()
    }
  }
}
</script>
<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
